<script>

import { PAGE_INFO } from '@/utils/MyUtils'

export default {
  name: 'BlogComment',
  data () {
    return {
      blogId: '',
      pageInfo: {
        ...PAGE_INFO
      },
      tableList: [],
      delIds: [],
      show: false
    }
  },
  methods: {
    init (blogId) {
      this.blogId = blogId
      this.pageInfo = { ...PAGE_INFO }

      this.getTableList()
    },
    getTableList () {
      this.$axios.get('comment/page', { params: { ...this.pageInfo, blogId: this.blogId } }).then(data => {
        if (data.total === 0) {
          this.$message.warning('暂无评论！')
          this.show = false
        } else {
          this.show = true
          this.tableList = [...data.records]
          this.pageInfo.count = data.total
        }
      })
    },
    currentChange (value) {
      this.pageInfo.page = value
      this.getTableList()
    },
    sizeChange (value) {
      this.pageInfo.page = 1
      this.pageInfo.limit = value
      this.getTableList()
    },
    selectionChange (selection) {
      this.delIds = [...selection.map(item => item.commentId)]
    },
    deleteData (id) {
      if (!id && this.delIds.length === 0) {
        this.$alert('请至少选中1条进行删除！', { type: 'warning' })
        return false
      }
      const deleteIds = id ? [id] : [...this.delIds]
      this.$confirm(`确定删除[${deleteIds}]的数据吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.delete('comment/delete', { data: deleteIds }).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      })
    },
    statusChange (id) {
      this.$confirm(`确定更改编号【${id}】的状态吗?`, { type: 'info', title: '询问' }).then(() => {
        this.$axios.post('comment/status/' + id).then(() => {
          this.$message.success('操作成功！')
          this.getTableList()
        }).catch(e => e)
      }).catch(e => e)
    }
  }
}
</script>

<template>
    <el-dialog v-if="show" title="博客评论" :visible.sync="show" width="80%">
      <el-button @click="deleteData()">删除</el-button>
      <el-table @selection-change="selectionChange" :data="tableList" border>
        <el-table-column type="selection" fixed="left"/>
        <el-table-column prop="commentId" label="编号"/>
        <el-table-column prop="name" label="评论人"/>
        <el-table-column prop="content" label="评论内容" show-overflow-tooltip/>
        <el-table-column prop="commentTime" label="评论时间"/>
        <el-table-column label="状态">
          <template slot-scope="{row}">
            <el-button @click="statusChange(row.commentId)" title="点击切换状态" type="success" v-if="row.status === 1">可用</el-button>
            <el-button @click="statusChange(row.commentId)" title="点击切换状态" type="danger" v-else>禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column fixed="right" width="180px" label="操作">
          <template slot-scope="{row}">
            <el-button @click="deleteData(row.commentId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="pageInfo.page" :page-size="pageInfo.limit" :page-sizes="[5,10,20,50,100]" :total="pageInfo.count" layout="total,prev,pager,next,jumper,sizes" @current-change="currentChange" @size-change="sizeChange"/>
    </el-dialog>
</template>

<style scoped lang="scss">

</style>
